package com.hhhn.wk.main.tab4.mymoney;

import android.content.Context;
import android.content.IntentFilter;
import android.graphics.drawable.ColorDrawable;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.hhhn.wk.R;
import com.hhhn.wk.base.BaseActivity;
import com.hhhn.wk.config.Config;
import com.hhhn.wk.main.tab4.allorder.adapter.ParentAdapter;
import com.hhhn.wk.main.tab4.allorder.entity.ItemGoodsBean;
import com.hhhn.wk.main.tab4.allorder.entity.MyOrder;
import com.hhhn.wk.main.tab4.allorder.fragment.Order01Fragment;
import com.hhhn.wk.main.tab4.mymoney.entity.RechargeDetail;
import com.hhhn.wk.utils.AllPublic;
import com.hhhn.wk.utils.Constants;
import com.hhhn.wk.utils.ToastUtils;
import com.hhhn.wk.utils.mi.AES;
import com.hhhn.wk.widget.adapter.MyAdaper;
import com.hhhn.wk.widget.adapter.ViewHolder;
import com.hhhn.wk.widget.wkpulltorefreshlayout.BaseRefreshListener;
import com.hhhn.wk.widget.wkpulltorefreshlayout.PullToRefreshLayout;
import com.hhhn.wk.widget.wkpulltorefreshlayout.ViewStatus;
import com.hhhn.wk.widget.wkpulltorefreshlayout.util.DisplayUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * 明细
 */
public class DetailActivity extends BaseActivity {
    private TextView tv_title;
    /*listView*/
    private ListView mLoadListView;
    private PullToRefreshLayout mPullToRefreshLayout;
    public static ArrayList<RechargeDetail> mArrayList = new ArrayList<RechargeDetail>();
        private static MyAdaper<RechargeDetail> mAdapter;
//    private ParentAdapter parentAdapter;
//    List<ItemGoodsBean> orderDetailVoList;//商品数组
    private int page = 1;
    private int refresh = 0;//0 第一次进入  1下拉刷新  2上啦加载
    private boolean isWU = false;
    private Context context;
    BaseActivity activity;
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    if (mArrayList.size() == 0) {
                        handler.sendEmptyMessage(2);
                    } else {
                        loadData();
                    }
                    break;
                case 2:
                    mPullToRefreshLayout.showView(ViewStatus.EMPTY_STATUS);////空数据
                    break;
                case 3:
                    mPullToRefreshLayout.showView(ViewStatus.ERROR_STATUS);////网络错误
                    break;
            }
            super.handleMessage(msg);
        }
    };
    @Override
    protected void setOnCreateContentView() {
        setContentView(R.layout.activity_detail);
        context = DetailActivity.this;
        activity = this;

        tv_title = findViewById(R.id.tv_title);
        mLoadListView = findViewById(R.id.mLoadListView);
        mPullToRefreshLayout = findViewById(R.id.activity_list);
        mLoadListView.setDivider(new ColorDrawable(getResources().getColor(R.color.white)));
        mLoadListView.setDividerHeight(DisplayUtil.dp2Px(context, 4));


    }

    @Override
    protected void initView() {
        tv_title.setText("充值明细");
    }

    @Override
    protected void initData() {
        getJSON();
    }

    @Override
    protected void initClick() {
        mPullToRefreshLayout.setRefreshListener(new BaseRefreshListener() {
            @Override
            public void refresh() {
                refresh = 1;
                page = 1;
//                orderDetailVoList.clear();
                mArrayList.clear();
                if (mAdapter != null) {
                    mAdapter.notifyDataSetChanged();
                }
                isWU = false;
                getJSON();
            }
            @Override
            public void loadMore() {
                if (isWU) {
                    ToastUtils.showShortToast(context, "暂无更多");
                    mPullToRefreshLayout.showView(ViewStatus.CONTENT_END);//暂无更多
                } else {
                    refresh = 2;
                    page++;
//                    if (parentAdapter != null) {
//                        parentAdapter.notifyDataSetChanged();
//                    }
                    getJSON();
                }
            }
        });
    }
    private void getJSON() {
        RequestParams params = AllPublic.getParamsPayRecharge(activity, "/queryRechargeOrder", false);
        try {
            JSONObject person=new JSONObject();
            person.put("cmUserId", activity.getUser().getUid());
            AES aes = AllPublic.jsonDesPackaging(person);
            params.addParameter("data",aes.getData());
            params.addParameter("key",aes.getKey());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        Log.d("@@", "params: " + params);
        //请求
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override  //成功
            public void onSuccess(String result) {
                Log.d("全部", result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String reason = jsonObject.getString("code");
                    if (reason.equals("0")) {//成功
                        JSONArray records = jsonObject.getJSONArray("data");
                        if (records.length() < Config.ROWS) {
                            isWU = true;
                        }

                        RechargeDetail mRechargeDetail;
                        for (int i = 0; i < records.length(); i++) {
                            JSONObject object = records.getJSONObject(i);
                            String id = object.has("id") ? object.getString("id") : "";//id


                            String type = object.has("payment") ? object.getString("payment") : "";
                            String number = object.has("money") ? object.getString("money") : "";
                            String time = object.has("cmIndate") ? object.getString("cmIndate") : "";

                            mRechargeDetail = new RechargeDetail();
                            mRechargeDetail.setNumber(number);
                            mRechargeDetail.setTime(time);
                            mRechargeDetail.setType(type);

                            mArrayList.add(mRechargeDetail);
                        }
                        handler.sendEmptyMessage(1);
                    } else {
                        Toast.makeText(activity, jsonObject.getString("message"), Toast.LENGTH_SHORT).show();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    handler.sendEmptyMessage(3);
                }
            }
            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.d("全部-第二个", ex + "");
                ex.printStackTrace();
                handler.sendEmptyMessage(3);
            }
            @Override
            public void onCancelled(CancelledException cex) {
                Log.d("全部-第三个", cex + "");
            }
            @Override
            public void onFinished() {
                Log.d("全部-第四个", "onFinished");
            }
        });
    }
    private void loadData() {
        if (mAdapter==null){
            mAdapter=new MyAdaper<RechargeDetail>(mArrayList,R.layout.item_detail) {
                @Override
                public void bindView(ViewHolder holder, RechargeDetail obj, int position) {
                    holder.setText(R.id.tv_type,obj.getType());
                    holder.setText(R.id.tv_time,obj.getTime());
                    holder.setText(R.id.tv_number,"+"+obj.getNumber());
                }
            };
            mLoadListView.setAdapter(mAdapter);
        }else {
            mAdapter.notifyDataSetChanged();
        }
        if (refresh == 1) {
            mPullToRefreshLayout.finishRefresh();//下拉刷新
        } else if (refresh == 2) {
            mPullToRefreshLayout.finishLoadMore();//上啦加载
        }
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mArrayList != null) {
            mArrayList.clear();
        }
        if (mAdapter != null) {
            mAdapter = null;
        }
    }
}
